{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 语言合成\n",
    "\n",
    "百度百科:语音合成是利用电子计算机和一些专门装置模拟人，制造语音的技术。\n",
    "\n",
    "百度AI语音合成产品介绍链接:https://ai.baidu.com/tech/speech/tts\n",
    "\n",
    "百度AI语音合成python接口使用说明:https://ai.baidu.com/ai-doc/SPEECH/Gk4nlz8tc\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 开始我们的实验\n",
    "## (1)登录百度AI开发平台，选择“语音技术”，使用“创建应用”建立一个语音技术应用。\n",
    "\n",
    "获取应用的AppID、API Key、Secret Key，注意:Secret Key要先选择显示，然后复制其现实的字符。\n",
    "\n",
    "例如我创建的语音识别应用获取的AppID、API Key、Secret Key如下：\n",
    "\n",
    "    \"\"\" 公开课版本的语音合成+人脸识别，可选更改为自己的api接口 \"\"\"\n",
    "    APP_ID = '20615915'\n",
    "    API_KEY = 'z2XGd5jZdcrpl01jzWGjuVRO'\n",
    "    SECRET_KEY = 'oLwTgla1eue2FLcBYWbsB6qRtDZC047d'\n",
    "\n",
    "注：对于“语音合成”功能来说，本步非必须，因为百度AI开发平台对于“语音合成”提供无限量调用试用。但如果想体验快速的“语音合成”功能，建议自己创建一个“语音合成”应用。我怕使用我这个应用的人太多，不能保证您的体验。\n",
    "\n",
    "百度AI开发平台注册链接:https://login.bce.baidu.com/?account=&redirect=http%3A%2F%2Fconsole.bce.baidu.com%2F%3Ffromai%3D1#/aip/overview\n",
    "\n",
    "## (2)熟悉我封装的百度AI语音库的模块\n",
    "\n",
    "在代码中输入如下代码，导入我的封装的模块\n",
    "\n",
    "    import sys\n",
    "    sys.path.append('../baidu_api_lib')\n",
    "    from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "封装库提供两个方法：初始化方法，文字到语音的语音合成方法。\n",
    "\n",
    "### <1>初始化方法，需要传入三个参数，就是上步我们创建“语音技术”应用获取的三个参数\n",
    "\n",
    "参数信息如下：\n",
    "\n",
    "    \"\"\" 公开课版本的语音合成+人脸识别，可选更改为自己的api接口 \"\"\"\n",
    "    APP_ID = '20615915'\n",
    "    API_KEY = 'z2XGd5jZdcrpl01jzWGjuVRO'\n",
    "    SECRET_KEY = 'oLwTgla1eue2FLcBYWbsB6qRtDZC047d'\n",
    "\n",
    "调用方式：\n",
    "\n",
    "    #传入百度AI的参数\n",
    "    word_2_sound = baidu_word_2_sound(APP_ID, API_KEY, SECRET_KEY)\n",
    "    \n",
    "    初始化完毕后，获取到word_2_sound对象，操作该对象就能进行语音合成。\n",
    "    \n",
    "### <2>文字到语音的语音合成方法\n",
    "\n",
    "这是我封装百度语音合成方法的函数原型：\n",
    "    \n",
    "    trans_word_to_sound(word, sound_file, sound_speed = 5, \\\n",
    "                            sound_tune = 5, sound_volume = 5, sound_type = 0)\n",
    "                            \n",
    "    trans_word_to_sound包含6个参数，其中word和sound_file为必须填写的内容。\n",
    "    \n",
    "    word:要语音合成的文字，需要用双引号包裹文字，例如:\"李白诗集\"，就是要合成李白诗集这个语音。\n",
    "    \n",
    "    sound_file:语音合成后，生成的音频文件，文件名需要用单引号包裹。例如:'tst_sound.mp3'，就是生成的语音存放在tst_sound.mp3文件中。\n",
    "    \n",
    "    sound_speed:语速，取值0-9，默认为5中语速。\n",
    "    \n",
    "    sound_tune:音调，取值0-9，默认为5中语调。\n",
    "    \n",
    "    sound_volume:音量，取值0-15，默认为5中音量。\n",
    "    \n",
    "    sound_type:发音人选择, 0为女声，1为男声，3为情感合成-度逍遥，4为情感合成-度丫丫，默认为普通女\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################\n",
    "#\n",
    "# 先感受下李白的诗集,选择本cell，按shirt+enter键运行本模块\n",
    "#\n",
    "######################################################\n",
    "\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "\"\"\" 公开课版本的语音合成+人脸识别，可选更改为自己的api接口 \"\"\"\n",
    "APP_ID = '20615915'\n",
    "API_KEY = 'z2XGd5jZdcrpl01jzWGjuVRO'\n",
    "SECRET_KEY = 'oLwTgla1eue2FLcBYWbsB6qRtDZC047d'\n",
    "\n",
    "#阅读的内容\n",
    "rd_poem = [\"李白诗集\",\\\n",
    "            \"静夜思。床前明月光，疑是地上霜。举头望明月，低头思故乡。\",\\\n",
    "            \"望庐山瀑布。日照香炉生紫烟，遥看瀑布挂前川。飞流直下三千尺，疑是银河落九天。\",\\\n",
    "            \"赠汪伦。李白乘舟将欲行，忽闻岸上踏歌声。桃花潭水深千尺，不及汪伦送我情。\",\\\n",
    "            \"早发白帝城。朝辞白帝彩云间，千里江陵一日还。两岸猿声啼不住，轻舟已过万重山。\"]\n",
    " \n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        #传入百度AI的参数\n",
    "        word_2_sound = baidu_word_2_sound(APP_ID, API_KEY, SECRET_KEY)\n",
    "            \n",
    "        #以迭代方式阅读\n",
    "        for one_poem in rd_poem:\n",
    "            #输出要语音合成的文字\n",
    "            print(one_poem)\n",
    "            \n",
    "            #进行语音合成\n",
    "            word_2_sound.trans_word_to_sound(one_poem,'tst_sound.wav')\n",
    "\n",
    "            #播放合成的语音\n",
    "            os.system('mplayer ' + 'tst_sound.wav')\n",
    "        \n",
    "        #删除临时文件    \n",
    "        os.remove('tst_sound.wav')\n",
    "        \n",
    "    except KeyboardInterrupt:\n",
    "        #删除临时文件    \n",
    "        os.remove('tst_sound.wav')\n",
    "        \n",
    "        print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 让我们临摹代码，学习“语音合成”功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################\n",
    "#\n",
    "# 让我们临摹代码，学习“语音合成”功能\n",
    "#\n",
    "######################################################\n",
    "\n",
    "#导入标准库\n",
    "#import sys\n",
    "\n",
    "\n",
    "#import os\n",
    "\n",
    "\n",
    "#导入自定义库\n",
    "#sys.path.append('../baidu_api_lib')\n",
    "\n",
    "\n",
    "#from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "\n",
    "# \"\"\" 公开课版本的语音合成+人脸识别，可选更改为自己的api接口 \"\"\"\n",
    "# APP_ID = '20615915'\n",
    "\n",
    "# API_KEY = 'z2XGd5jZdcrpl01jzWGjuVRO'\n",
    "\n",
    "# SECRET_KEY = 'oLwTgla1eue2FLcBYWbsB6qRtDZC047d'\n",
    "\n",
    "\n",
    "\n",
    "#阅读的内容\n",
    "#rd_poem = [\"李白诗集\",\"静夜思。床前明月光，疑是地上霜。举头望明月，低头思故乡。\"]\n",
    "\n",
    "#python模块运行入口点，python从此处开始运行程序 \n",
    "#if __name__ == \"__main__\":\n",
    "\n",
    "\n",
    "#    try:\n",
    "\n",
    "\n",
    "        #传入百度AI的参数\n",
    "        #word_2_sound = baidu_word_2_sound(APP_ID, API_KEY, SECRET_KEY)\n",
    "        \n",
    "        \n",
    "            \n",
    "        #以迭代方式阅读\n",
    "        #for one_poem in rd_poem:\n",
    "        \n",
    "            #输出要语音合成的文字\n",
    "            #print(one_poem)\n",
    "        \n",
    "        \n",
    "            #进行语音合成\n",
    "            #word_2_sound.trans_word_to_sound(one_poem,'tst_sound.wav',sound_type = 4)\n",
    "            \n",
    "\n",
    "            #播放合成的语音\n",
    "            #os.system('mplayer ' + 'tst_sound.wav')\n",
    "            \n",
    "        \n",
    "        #删除临时文件    \n",
    "        #os.remove('tst_sound.wav')\n",
    "        \n",
    "        \n",
    "    #except KeyboardInterrupt:\n",
    "        #删除临时文件    \n",
    "        #os.remove('tst_sound.wav')\n",
    "        \n",
    "        #print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课后练习\n",
    "\n",
    "## 1.当前我们使用默认的语速、音调、音量、发音人选择。\n",
    "\n",
    "我们尝试更改上述参数，体验不同的声音的“语音合成”\n",
    "\n",
    "    sound_speed:语速，取值0-9，默认为5中语速。\n",
    "    sound_tune:音调，取值0-9，默认为5中语调。\n",
    "    sound_volume:音量，取值0-15，默认为5中音量。\n",
    "    sound_type:发音人选择, 0为女声，1为男声，3为情感合成-度逍遥，4为情感合成-度丫丫，默认为普通女\n",
    "\n",
    "## 2.更改诗词内容，播放自己的诗词\n",
    "\n",
    "修改rd_poem对象后的内容，播放自己喜欢的诗词。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################\n",
    "#\n",
    "# 更改参数，体验不同的“语音合成”\n",
    "#\n",
    "######################################################\n",
    "\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "\"\"\" 公开课版本的语音合成+人脸识别，可选更改为自己的api接口 \"\"\"\n",
    "APP_ID = '20615915'\n",
    "API_KEY = 'z2XGd5jZdcrpl01jzWGjuVRO'\n",
    "SECRET_KEY = 'oLwTgla1eue2FLcBYWbsB6qRtDZC047d'\n",
    "\n",
    "#阅读的内容\n",
    "rd_poem = [\"李白诗集\",\\\n",
    "            \"静夜思。床前明月光，疑是地上霜。举头望明月，低头思故乡。\",\\\n",
    "            \"望庐山瀑布。日照香炉生紫烟，遥看瀑布挂前川。飞流直下三千尺，疑是银河落九天。\",\\\n",
    "            \"赠汪伦。李白乘舟将欲行，忽闻岸上踏歌声。桃花潭水深千尺，不及汪伦送我情。\",\\\n",
    "            \"早发白帝城。朝辞白帝彩云间，千里江陵一日还。两岸猿声啼不住，轻舟已过万重山。\"]\n",
    " \n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        #传入百度AI的参数\n",
    "        word_2_sound = baidu_word_2_sound(APP_ID, API_KEY, SECRET_KEY)\n",
    "            \n",
    "        #以迭代方式阅读\n",
    "        for one_poem in rd_poem:\n",
    "            #输出要语音合成的文字\n",
    "            print(one_poem)\n",
    "            \n",
    "            #默认语音合成\n",
    "            #word_2_sound.trans_word_to_sound(one_poem,'tst_sound.wav')\n",
    "            \n",
    "            #更改语速的语音合成\n",
    "            #word_2_sound.trans_word_to_sound(one_poem,'tst_sound.wav',sound_speed = 8)\n",
    "            \n",
    "            #更改语调的语音合成\n",
    "            #word_2_sound.trans_word_to_sound(one_poem,'tst_sound.wav',sound_tune = 8)\n",
    "            \n",
    "            #更改音量的语音合成\n",
    "            #word_2_sound.trans_word_to_sound(one_poem,'tst_sound.wav',sound_volume = 8)\n",
    "            \n",
    "            #更改发音人选择的语音合成\n",
    "            word_2_sound.trans_word_to_sound(one_poem,'tst_sound.wav',sound_type = 4)\n",
    "            \n",
    "            #复合更改参数，进行语音合成\n",
    "            #word_2_sound.trans_word_to_sound(one_poem,'tst_sound.wav',sound_speed = 8,\\\n",
    "            #                                 sound_tune = 8, sound_volume = 8, sound_type = 4)\n",
    "\n",
    "            #播放合成的语音\n",
    "            os.system('mplayer ' + 'tst_sound.wav')\n",
    "        \n",
    "        #删除临时文件    \n",
    "        os.remove('tst_sound.wav')\n",
    "        \n",
    "    except KeyboardInterrupt:\n",
    "        #删除临时文件    \n",
    "        os.remove('tst_sound.wav')\n",
    "        print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
